<?php

namespace app\api\controller\homepage;

use app\admin\model\AdminBasicZoom;
use app\admin\model\AdminFollow;

use app\common\controller\Api;
use app\common\library\Iden;


// 允许跨域
header('Access-Control-Allow-Origin:*');//允许跨域

/**
 * 个人中心
 * 用户关注
 */
class Follow extends Api
{

    //如果$noNeedLogin为空表示所有接口都需要登录才能请求
    //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
    //如果接口已经设置无需登录,那也就无需鉴权了
    //
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];


    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\AdminFollow;
    }




     /**
     * @desc 个人关注的活动或系列列表页
     * @param $uid
     * */
    public function list()
    {
        $uInfo = $this->auth->getUserInfo();
        if (empty($uInfo)) {//未登录
            $this->error(__('Please login first'), null, 401);
        }
        $admin_id = Iden::decrypt($uInfo['id']);
        if (empty($admin_id)) {
            $this->error(__('获取用户参数出错'));
        }
        //$admin_id = 31896;
        $map['operate_uid'] = intval($admin_id);

        $per_page = $this->request->get('per_page', 1);
        $limit = (int)$this->request->get('limit', 20);
        $offset = ($per_page - 1) * $limit;
        //当前是否为关联查询
        $this->relationSearch = true;
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);

        $list = $this->model
            ->with(['admin'])
            ->where($map)
            ->order('ctime DESC')
            ->limit($offset, $limit);
        $list = $list->select();
        $total = count($list);
        foreach ($list as $k => $row) {
            $row->visible(['username', 'nickname', 'avatar','uid']);
            $row->getRelation('admin')->visible(['id', 'username', 'nickname', 'mobile']);
            $list[$k]['uid'] = Iden::encrypt($row['admin']['id']);
            //$list[$k]['username'] = $row['admin']['username'];
            $list[$k]['nickname'] = $row['admin']['nickname'];

            $uInfo = AdminBasicZoom::getAdminInfoById($row['admin']['id']);
            if($uInfo){
                $list[$k]['avatar'] = $uInfo['avatar'];
            }else{
                $list[$k]['avatar'] = '';
            }


        }

        $result = array("total" => $total, "rows" => $list);

        $this->success(__('读取成功'), $result,200);

    }




    /*
     * 关注操作
     */
    public function operate(){
        $uInfo = $this->auth->getUserInfo();
        if(empty($uInfo)){//未登录
            $this->error(__('Please login first'), null, 401);
        }
        $admin_id = Iden::decrypt($uInfo['id']);
        if(empty($admin_id)){
            $this->error(__('获取用户参数出错'));
        }
        //$admin_id = 31896;
        $uid = $this->request->param('uid');//被关注人uid
        if(empty($uid)){
            $this->error('没有获取到活动id,请重试');
        }
        $uid = Iden::decrypt($uid);
        if(empty($uid)){
            $this->error('参数出错');
        }

        $ftype = $this->request->param('ftype');//关注类型 1 关注 2 取消关注

        if(empty($ftype)){
            $this->error('获取关注类型出错');
        }
        if(!in_array($ftype,[1,2])){
            $this->error('获取关注类型值有误,请重试');
        }

        if($ftype == 1){//关注
            $row = $this->model->get(['operate_uid' => $admin_id,'follow_uid'=>$uid]);
            if ($row) {
                $this->error('此举办人已关注','',0 );
            }

            $data['operate_uid'] = $admin_id;
            $data['follow_uid'] = $uid;//被关注人
            $data['ctime'] = time();
            $follow =  new AdminFollow();
            $r = $follow->save($data);
            if (!$r){
                $this->error('关注失败，请重试', '', 0);
            }

            unset($data);
        }else{//取消关注
            $row = $this->model->get(['operate_uid' => $admin_id,'follow_uid'=>$uid]);
            if (!$row) {
                $this->error('没有找到此关注人','',0 );
            }
            $r = $this->model->where(['operate_uid' => $admin_id,'follow_uid'=>$uid])->delete();
            if(!$r){
                $this->error('取消关注失败，请重试', '', 0);
            }
        }

        $this->success(__('Follow successful'), '',200);
    }

}
